package com.itestor.iptv.modules.common.dao;

import org.hibernate.Query;
import org.hibernate.Session;

import com.itestor.iptv.global.dao.GenericHibernateDao;
import com.itestor.iptv.modules.common.domain.GroupManage;
import com.itestor.iptv.modules.common.domain.RobotGroupManage;

/**
 * @author Mr.li
 */
public class TbGroupManageDao extends GenericHibernateDao<GroupManage, Integer> {

    /**
     * 按用户名查找.
     * @param groupName 虚拟分组名称.
     * @return TbUser 用户
     */
    public GroupManage find(String groupName) {
        StringBuilder hql = new StringBuilder();
        hql.append("from GroupManage t where t.groupName=:groupName");
        Query query = getHibernateSession().createQuery(hql.toString());
        query.setParameter("groupName", groupName);

        return getEntity(query);
    }

    /**
     * 创建聚合组.
     * @param pIdGroup
     * @param pRobotId
     */
    public void mergeGroupManageRobotRelation(int pIdGroup, Integer pRobotId) {
        RobotGroupManage relation = new RobotGroupManage();
        relation.setIdGroup(pIdGroup);
        relation.setIdRobot(pRobotId);
        
        Session session = getHibernateSession();
        session.saveOrUpdate(relation);
    }
    /**
     * 删除关联设备
     * @param idGroup
     * @param pId
     */
    public void deleteRelRobot(int idGroup, int pId) {
        StringBuilder hql = new StringBuilder();
        hql.append("delete from RobotGroupManage t where t.idGroup=:idGroup and t.idRobot=:idRobot");
        Query query = getHibernateSession().createQuery(hql.toString());
        
        query.setParameter("idGroup", idGroup);
        query.setParameter("idRobot", pId);
        
        query.executeUpdate();     
    }

}